<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/10
 * Time: 23:47
 */

namespace app\index\controller;


use app\index\Model\Instock;
use app\index\Model\Instock_item;
use app\index\Model\Sale;
use app\index\Model\Stock_pile;
use think\Request;
use think\session;
class StockpileController extends BaseController
{
    public function index(){
        $number = Request::instance()->param('number/d');
        $Stockpile = new Stock_pile();
        $stockpile = $Stockpile->where('status',0)->order('number','desc')->select();

        if (Request::instance()->isAjax()){
            $data = Stock_pile::get(['number'=>$number,]);
            if ($data){
                return json(['code'=>'1','data'=>$data,'msg'=>'数据提交成功']);
            }else{
                return json(['code'=>'504','data'=>'error','msg'=>'数据不存在']);
            }
        }else{
            $this->assign('stockpile',$stockpile);

            return $this->fetch();

        }

    }

    public function sale(){
        $number = Request::instance()->param('number/d');
        if (Request::instance()->isAjax()){
            $data = Stock_pile::get(['number'=>$number,'status'=>0]);
            if ($data){
                return json(['code'=>'1','data'=>$data,'msg'=>'数据提交成功']);
            }else{
                return json(['code'=>'504','data'=>'error','msg'=>'数据不存在']);
            }
        }
    }

    public function item(){
        $number = Request::instance()->param('number');
        $Instock = new Instock();
        $Sale = new Sale();
        $instock = $Instock->where(['number'=>$number])->select();
        $sale = $Sale->where(['number'=>$number])->select();
        $instock_quantity = '';
        $sale_quantity = '';
        foreach ($instock as $_instock){
            $instock_quantity += $_instock['quantity'];
        }
        foreach ($sale as $_sale){
            $sale_quantity += $_sale['quantity'];
        }
        $this->assign('instock',$instock);
        $this->assign('sale',$sale);
        $this->assign('instock_quantity',$instock_quantity);
        $this->assign('sale_quantity',$sale_quantity);
        return $this->fetch();
    }
}